<?php

function httpRequest($ip, $host ,$request_uri)
{
	if(($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
		return false;
	}
	//set socket timeout
	socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec"=>20, "usec"=>0));
        if(($result = socket_connect($socket, $ip ,80)) != 1)
        {
                socket_close($socket);
                return false;
        }

	//GET method
	$in = "GET " . $request_uri . " HTTP/1.1\r\n";
	$in .= "Accept: */*\r\n";
	$in .= "User-Agent: Lowell-Agent\r\n";
	$in .= "Host: " . $host . "\r\n";
	$in .= "Connection: Close\r\n\r\n";
	if(!socket_write($socket, $in, strlen($in))){
		socket_close($socket);
		return false;
	}
	unset($in);

	//process response
	$out = "";
	while($buff = @socket_read($socket, 2048)) {
		$out .= $buff;
	}
	//finish socket
	socket_close($socket);
	$pos    = strpos($out, "\r\n\r\n");
	$head   = substr($out, 0, $pos);		                //http head
	$status = substr($head, 0, strpos($head, "\r\n"));		//http status line
	$body   = substr($out, $pos + 4, strlen($out) - ($pos + 4));    //page body
	if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches))
        {
		if(intval($matches[1]) / 100 == 2) {
			return $body;
		} else {
			return false;
		}
	} else {
		return false;
	}
}



?>